package cn.iocoder.yudao.module.handguard.service.publicitytype;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.publicitytype.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.publicitytype.PublicityTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.publicitytype.PublicityTypeConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.publicitytype.PublicityTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 16个公示 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PublicityTypeServiceImpl implements PublicityTypeService {

    @Resource
    private PublicityTypeMapper publicityTypeMapper;

    @Override
    public Long createPublicityType(PublicityTypeCreateReqVO createReqVO) {
        // 插入
        PublicityTypeDO publicityType = PublicityTypeConvert.INSTANCE.convert(createReqVO);
        publicityTypeMapper.insert(publicityType);
        // 返回
        return publicityType.getId();
    }

    @Override
    public void updatePublicityType(PublicityTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validatePublicityTypeExists(updateReqVO.getId());
        // 更新
        PublicityTypeDO updateObj = PublicityTypeConvert.INSTANCE.convert(updateReqVO);
        publicityTypeMapper.updateById(updateObj);
    }

    @Override
    public void deletePublicityType(Long id) {
        // 校验存在
        validatePublicityTypeExists(id);
        // 删除
        publicityTypeMapper.deleteById(id);
    }

    private void validatePublicityTypeExists(Long id) {
        if (publicityTypeMapper.selectById(id) == null) {
            throw exception(PUBLICITY_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public PublicityTypeDO getPublicityType(Long id) {
        return publicityTypeMapper.selectById(id);
    }

    @Override
    public List<PublicityTypeDO> getPublicityTypeList() {
        LambdaQueryWrapper<PublicityTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(PublicityTypeDO::getId);
        List<PublicityTypeDO> doList = publicityTypeMapper.selectList(queryWrapper);
        return doList;
    }

    @Override
    public PageResult<PublicityTypeDO> getPublicityTypePage(PublicityTypePageReqVO pageReqVO) {
        return publicityTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PublicitySelectBaseVO> getPublicitySelectListByClassName(String className,String typesName) {
        return publicityTypeMapper.getPublicitySelectListByClassName(className,typesName);
    }

    @Override
    public List<PublicitySelectBaseVO> getPublicitySelectListByTypesName(String typesName,Long orderId) {
        List<PublicitySelectBaseVO> list = publicityTypeMapper.getPublicitySelectListByTypesName(typesName,orderId);
        list.forEach(i -> {
            if (i.getCategory().equals("200")) {
                if (StringUtils.isNotEmpty(i.getResultValue())) {
                    if (i.getResultValue().contains("-")) {
                        String[] arrStr = i.getResultValue().split("-");
                        i.setParam1(arrStr[0]);
                        i.setParam2(arrStr[1]);
                    }else {
                        i.setParam1(i.getResultValue());
                    }
                }
            } else if (i.getCategory().equals("201")) {
                if (StringUtils.isNotEmpty(i.getResultValue())) {
                    if (i.getResultValue().contains(",")) {
                        String[] vArr = i.getResultValue().split(",");
                        String[] arrStr1 = vArr[0].split("-");
                        String[] arrStr2 = vArr[1].split("-");
                        i.setParam1(arrStr1[0]);
                        i.setParam2(arrStr1[1]);
                        i.setParam3(arrStr2[0]);
                        i.setParam4(arrStr2[1]);
                    }else {
                        String[] arrStr1 = i.getResultValue().split("-");
                        i.setParam1(arrStr1[0]);
                        i.setParam3(arrStr1[1]);
                    }
                }
            }else  if (i.getCategory().equals("301")) {
                if (StringUtils.isNotEmpty(i.getResultValue())){
                    i.setValueArr(i.getResultValue().split(","));
                }
            }
        });
        return list;
    }

    @Override
    public List<PublicityTypeDO> getPublicityTypeList(PublicityTypeExportReqVO exportReqVO) {
        return publicityTypeMapper.selectList(exportReqVO);
    }

}
